package com.kaisa.activity.ui;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.WelcomeAdapter;
import com.kaisa.activity.bean.ProductDetailBean;
import com.kaisa.activity.bean.ProductImgBean;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ProductDetailsActivity extends AppCompatActivity {

    private String entity_id;
    private String tourGroupId;

    //UI
    @ViewInject(R.id.vp_product)
    private ViewPager productPger;
    @ViewInject(R.id.ll_dot_product)
    private LinearLayout linearLayout;

    @ViewInject(R.id.tv_product_lineName)
    private TextView lineName;
    @ViewInject(R.id.tv_product_lineTypeName)
    private TextView lineTypeName;
    @ViewInject(R.id.tv_product_presalePrice)
    private TextView presalePrice;
    @ViewInject(R.id.tv_product_remainSignUpNum)
    private TextView remainSignUpNum;
    @ViewInject(R.id.tv_product_retailPrice)
    private TextView retailPrice;
    @ViewInject(R.id.tv_product_next)
    private TextView next;
    @ViewInject(R.id.tv_product_startDate)
    private TextView startDate;
    @ViewInject(R.id.tv_product_departureName)
    private TextView departureName;
    @ViewInject(R.id.tv_product_toairlineCompany)
    private TextView toairlineCompany;
    @ViewInject(R.id.tv_product_tolineInfoFlight)
    private TextView tolineInfoFlight;
    @ViewInject(R.id.tv_product_backairlineCompany)
    private TextView backairlineCompany;
    @ViewInject(R.id.tv_product_backlineInfoFlight)
    private TextView backlineInfoFlight;
    @ViewInject(R.id.tv_product_tourGroupPeopleNum)
    private TextView tourGroupPeopleNum;
    @ViewInject(R.id.tv_product_join)
    private TextView join;
    @ViewInject(R.id.tv_product_score)
    private TextView score;
    @ViewInject(R.id.tv_product_joinpeoplenum)
    private TextView joinpeoplenum;
    @ViewInject(R.id.tv_product_visa)
    private TextView visa;
    @ViewInject(R.id.tv_product_lineFeature)
    private TextView lineFeature;
    @ViewInject(R.id.tv_product_attributes)
    private TextView attributes;

    private WelcomeAdapter imgAdapter;
    private List<ImageView> imgList = new ArrayList<ImageView>();
    private List<ProductImgBean.MatchesEntity> matchesEntityList = new ArrayList<ProductImgBean.MatchesEntity>();
    private List<ProductDetailBean.MatchesEntity> detailList = new ArrayList<ProductDetailBean.MatchesEntity>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);
        x.view().inject(this);
        Intent intent = getIntent();
        entity_id = intent.getStringExtra("entity_id");
        tourGroupId = intent.getStringExtra("tourGroupId");
        initView();
        initData();
        addLinstener();

    }


    private void initView() {
        imgAdapter = new WelcomeAdapter(imgList);
        productPger.setAdapter(imgAdapter);

    }

    private void initData() {
        //下载图片地址
        x.http().get(getParams(0), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                ProductImgBean bean = JSONObject.parseObject(s, ProductImgBean.class);
                matchesEntityList.addAll(bean.getMatches());
                for (int i = 0; i < matchesEntityList.size(); i++) {
                    final ImageView iv = new ImageView(ProductDetailsActivity.this);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
                    iv.setLayoutParams(params);
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    iv.setBackgroundResource(R.mipmap.moren);
                    x.image().bind(iv, matchesEntityList.get(i).getEntityUrl());
                    imgList.add(iv);
                }
                for (int i = 0; i < matchesEntityList.size(); i++) {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    ImageView ivDot = new ImageView(ProductDetailsActivity.this);
                    ivDot.setLayoutParams(params);
                    ivDot.setScaleType(ImageView.ScaleType.FIT_XY);
                    ivDot.setImageResource(R.drawable.bg_dots);
                    linearLayout.addView(ivDot);
                }
                linearLayout.getChildAt(0).setEnabled(false);
                imgAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });

        //下载数据详情
        x.http().get(getParams(1), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                ProductDetailBean detailbean = JSONObject.parseObject(s, ProductDetailBean.class);
                detailList.addAll(detailbean.getMatches());
                addData();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void addLinstener() {
        productPger.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imgList.size(); i++) {
                    linearLayout.getChildAt(i).setEnabled(true);
                }
                linearLayout.getChildAt(position).setEnabled(false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addData() {
        lineName.setText(detailList.get(0).getLineName());
        lineTypeName.setText(detailList.get(0).getLineTypeName());
        presalePrice.setText(detailList.get(0).getPresalePrice());
        remainSignUpNum.setText(detailList.get(0).getRemainSignUpNum());
        retailPrice.setText(detailList.get(0).getRetailPrice());
        retailPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        startDate.setText("最近出团  " + detailList.get(0).getStartDate());
        departureName.setText("出发地        " + detailList.get(0).getDepartureName());
        toairlineCompany.setText(detailList.get(0).getAttributes().getSource_1().get(0).getAirlineCompany());
        tolineInfoFlight.setText(detailList.get(0).getAttributes().getSource_1().get(0).getLineInfoFlight());
        backairlineCompany.setText(detailList.get(0).getAttributes().getSource_1().get(detailList.get(0).getAttributes().getSource_1().size() - 1).getAirlineCompany());
        backlineInfoFlight.setText(detailList.get(0).getAttributes().getSource_1().get(detailList.get(0).getAttributes().getSource_1().size() - 1).getLineInfoFlight());
        tourGroupPeopleNum.setText("团队规模  " + detailList.get(0).getTourGroupPeopleNum() + " 已报");
        join.setText(Integer.valueOf(detailList.get(0).getTourGroupPeopleNum()) - Integer.valueOf(detailList.get(0).getRemainSignUpNum()) + "  ");
        score.setText(detailList.get(0).getScore());
        joinpeoplenum.setText(detailList.get(0).getJoinpeoplenum());
        lineFeature.setText(detailList.get(0).getLineFeature());
        visa.setText(detailList.get(0).getVisa());

    }


    public RequestParams getParams(int tag) {
        RequestParams params;
        if (tag == 0) {
            params = new RequestParams(UrlHttp.PATH_PRODUCT_IMG);
            params.addBodyParameter("entity_id", entity_id);
        } else {
            params = new RequestParams(UrlHttp.PATH_PRODUCT_DETAILS);
            params.addBodyParameter("tourGroupId", tourGroupId);
            params.addBodyParameter("companyId", "178");
        }

        params.addBodyParameter("account", "123");
        params.addBodyParameter("channel", "CS0001");
        params.addBodyParameter("channelSecond", "a0002");
        params.addBodyParameter("password", "123");
        params.addBodyParameter("terminal", "android");
        params.addBodyParameter("sourceFlag", "860806020832282");
        return params;
    }

}
